package com.studio.framework.controller.monitorSystem.service;

import com.studio.framework.controller.monitorSystem.bean.ServerList;
import com.studio.framework.controller.monitorSystem.dto.ServerDto;
import com.studio.framework.controller.monitorSystem.repository.ServerListRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author jujh
 * @Date 2018/7/23
 */
@Service
public class ServerService {

    @Autowired
    private ServerListRepository serverListRepository;


    /**
     * 条件查询所有的服务
     *
     * @return
     */
    public List<ServerList> findAllByCondition() {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        return serverListRepository.findAll(sort);
    }

    /**
     * add
     *
     * @return
     */
    public String create(ServerDto createServerDto) {
        ServerList serverList = new ServerList();
        BeanUtils.copyProperties(createServerDto, serverList);
        serverList.setCreateDate(new Date());
        serverList = serverListRepository.save(serverList);
        return serverList.getId();
    }

    /**
     * update
     *
     * @return
     */
    public int update(ServerDto serverDto) {
        ServerList serverList = findById(serverDto.getId());
        if (serverList == null) {
            return 1;
        } else {
            copyPropertiesIgnoreNull(serverDto, serverList);
            serverListRepository.save(serverList);
        }
        return 0;
    }

    /**
     * delete
     *
     * @return
     */
    public int delete(String id) {
        ServerList serverList = findById(id);
        if (serverList == null) {
            return 1;
        } else {
            serverListRepository.delete(id);
            return 0;
        }
    }

    /**
     * 查询详情
     *
     * @return
     */
    public ServerList findById(String id) {
        return serverListRepository.findOne(id);
    }

    /**
     * update
     *
     * @return
     */
    public long count(String groupId) {
        ServerList severList = new ServerList();
        Example<ServerList> example = Example.of(severList);
        severList.setGroupId(groupId);
        return serverListRepository.count(example);
    }

    private static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    private static void copyPropertiesIgnoreNull(Object src, Object target) {
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }


}
